package com.ruoyi.rms.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.rms.enums.WaitStatus;
import com.ruoyi.rms.utils.IdUtils;
import com.ruoyi.rms.utils.RetUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rms.mapper.WaitMessageMapper;
import com.ruoyi.rms.domain.WaitMessage;
import com.ruoyi.rms.service.IWaitMessageService;

/**
 * 排队信息Service业务层处理
 * 
 * @author wyl
 * @date 2024-03-08
 */
@Service
@Slf4j
public class WaitMessageServiceImpl implements IWaitMessageService 
{
    @Autowired
    private WaitMessageMapper waitMessageMapper;

    /**
     * 查询排队信息
     * 
     * @param waitId 排队信息主键
     * @return 排队信息
     */
    @Override
    public WaitMessage selectWaitMessageByWaitId(Long waitId)
    {
        return waitMessageMapper.selectWaitMessageByWaitId(waitId);
    }

    /**
     * 查询排队信息列表
     * 
     * @param waitMessage 排队信息
     * @return 排队信息
     */
    @Override
    public List<WaitMessage> selectWaitMessageList(WaitMessage waitMessage)
    {
        return waitMessageMapper.selectWaitMessageList(waitMessage);
    }

    /**
     * 新增排队信息
     * 
     * @param waitMessage 排队信息
     * @return 结果
     */
    @Override
    public int insertWaitMessage(WaitMessage waitMessage)
    {
        // 保证系统中每人只能有一条未结束的预约
        WaitMessage wait = new WaitMessage();
        wait.setWxId(waitMessage.getWxId());
        wait.setStatus(WaitStatus.WAITING.getStatus());
        List<WaitMessage> waitMessageList = waitMessageMapper.selectWaitMessageList(wait);
        if (!waitMessageList.isEmpty()) {
            log.error("此用户有未结束的预约，预约失败");
            return RetUtils.ERROR;
        }

        waitMessage.setWaitId(IdUtils.createId());
        waitMessage.setWaitStartTime(DateUtils.getNowDate());
        waitMessage.setStatus(WaitStatus.WAITING.getStatus()); // 默认状态：排队中
        // 计算排队号
        waitMessage.setWaitNo(getWaitNo());
        return waitMessageMapper.insertWaitMessage(waitMessage);
    }

    /**
     * 修改排队信息
     * 
     * @param waitMessage 排队信息
     * @return 结果
     */
    @Override
    public int updateWaitMessage(WaitMessage waitMessage)
    {
        return waitMessageMapper.updateWaitMessage(waitMessage);
    }

    /**
     * 批量删除排队信息
     * 
     * @param waitIds 需要删除的排队信息主键
     * @return 结果
     */
    @Override
    public int deleteWaitMessageByWaitIds(Long[] waitIds)
    {
        return waitMessageMapper.deleteWaitMessageByWaitIds(waitIds);
    }

    /**
     * 删除排队信息信息
     * 
     * @param waitId 排队信息主键
     * @return 结果
     */
    @Override
    public int deleteWaitMessageByWaitId(Long waitId)
    {
        return waitMessageMapper.deleteWaitMessageByWaitId(waitId);
    }

    /**
     * 计算排队号
     * @return
     */
    private synchronized Integer getWaitNo() {
        Integer waitNo = waitMessageMapper.getWaitNo(DateUtils.getDate());
        if (waitNo == null) return 1;
        else return waitNo + 1;
    }
}
